﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace myWindowsMediaPlayer
{
    public class MediaElementViewModel : DependencyObject, INotifyPropertyChanged
    {
        public MediaElementViewModel(MediaElement media)
        {
            // TODO: Complete member initialization
            this.mediaView = media;
            this.LoadedBehavior = MediaState.Manual;
            this.mediaCmd = new MediaElementModel(this);
            this.canExecuteCommand = true;
            this.Source = "";
            this.Current = "";
            this.mediaView.Visibility = Visibility.Hidden;
            this.libraryCmd = new LibraryModel(this);
            this.Seek = 0;
//            library = new ObservableCollection<LibraryModel>();
        }

        public MediaElementModel mediaCmd { set; get; }
        public MediaElement mediaView { set; get; }
        public bool canExecuteCommand { set; get; }
        private double _Seek;
        public double Seek
        {
            get
            {
                return this._Seek;
            }
            set
            {
                _Seek = mediaView.Position.Seconds;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Seek"));
            }
        }
        private string _Current;
        public string Current
        {
            get
            {
                return this._Current;
            }
            set
            {
                if (library != null)
                {
                    foreach (PlaylistItem item in library)
                    {
                        if (item._path == Source)
                        {
                            _Current = (item._artist.Length > 0 ? item._artist + " - " : "") + item._title;
                            break;
                        }
                    }
                    if (this.PropertyChanged != null)
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Current"));
                }
            }
        }
        private string _Source;
        public string Source
        {
            get
            {
                return this._Source;
            }
            set
            {
                if (this._Source != value)
                {
                    this._Source = value;
                    this.Current = _Source;
                    if (this.PropertyChanged != null)
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Source"));
                }
            }
        }
        public MediaState LoadedBehavior { set; get; }
        public LibraryModel libraryCmd { set; get; }
        private ObservableCollection<PlaylistItem> _library;
        public ObservableCollection<PlaylistItem> library
        {
            get
            {
                return this._library;
            }
            set
            {
                if (this._library != value)
                {
                    this._library = value;
                    if (this.PropertyChanged != null)
                        this.PropertyChanged(this, new PropertyChangedEventArgs("library"));
                }
            }
        }
        // Une collection pour la playlist en cours.
        // Une collection pour la bibliothèque Video
        // Une collection pour la bibliothèque Audio
        // playlistModel pour la playlist
        // bibliothèqueVideoModel
        // bibliothèqueAudioModel

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
